package cn.jathlon.datastructure;

import gnu.trove.map.hash.TObjectIntHashMap;

import java.util.Map;
import java.util.Set;

/**
 * 独立Bag类, maintaining a allCount variable of the collection.
 * @author tuxiao.czz
 *
 * @param <E>
 */
public class Bag<E> {
	
	//final Map<E, Integer> map;
	
	final TObjectIntHashMap<E> map;  ////2012-12-14 11:10:56
	
	private int allCount;  ///all count!
	
	public int getAllCount() {
		return allCount;
	}
	////////////////Construction method
	public Bag() {
		//map = new HashMap<E, Integer>();
		this.map = new TObjectIntHashMap<E>();
		allCount = 0;
	}

	////////////////Construction method
	public Bag(Map<E, Integer> map) {
		this.map = new TObjectIntHashMap<E>();
		if(map != null){
			for (Map.Entry<E, Integer> e : map.entrySet()) {
				add(e.getKey(), e.getValue());
			}
		}
	}

	public void add(E e) {
		add(e, 1);
	}

	public void add(E e, int count) {
		map.put(e, map.containsKey(e) ? map.get(e) + count : count);
		allCount += count;
	}

	public Set<E> keySet() {
		return map.keySet();
	}

	public Object[] keys() {
		return map.keys();
	}
	
	public int[] values() {
		return map.values();
	}

	public int getCount(E e) {
		return map.containsKey(e) ? map.get(e) : 0;
	}

	
	
}


